package com.softserveinc.autorent.model;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;



public class Main {
	private static User u2;

	public void ejbMethodOrServletServiceMethod() {
		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("jpa");
		EntityManager em = emf.createEntityManager();

		try {


			UserDriveLicense l = new UserDriveLicense();
			l.setNumber(345345435);

			UserInfo i = new UserInfo();
			i.setPassport("1221323");

			User u = new User();
			u.setFirstName("Dima");
			u.setLastName("Karabat");
			u.setInfo(i);
			u.setLicense(l);
			u.setRole(UserRoles.ADMINISTRATOR);

			CarVendor v = new CarVendor();
			v.setName("Mersedes");

			// CarPhoto cp = new CarPhoto();
			// cp.setPhotoName("front");

			CarModel cm = new CarModel();
			cm.setName("Compressor");
			cm.setCarVendor(v);


			Car car = new Car();			
			car.setNumber("123123");
			car.setVin("123123");
			car.setCarModel(cm);
			car.setCarStatus(CarStatusEnum.FREE);
			car.setCarClass("etfe");
			car.setColor("red");

			Order o = new Order();
			o.setCar(car);
			o.setUser(u);
			o.setOrderStatus(OrderStatusEnum.NEW);

			em.getTransaction().begin();
			em.persist(i);
			em.persist(l);
			em.persist(u);

			em.persist(v);
			em.persist(cm);
			em.persist(car);

			em.persist(o);
			em.getTransaction().commit();

			u2 = new User();
			em.getTransaction().begin();
			u2 = em.find(User.class, 2);
			System.out.println(u2.getFirstName() );
			em.getTransaction().commit();
			
//			
			final String className = Car.class.getName();
			Query q = em.createQuery("from " + className + " c");
			Collection<Car> lst = q.getResultList();
			

		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			em.close();
		}

	}

	public static void main(String args[]) {
		new Main().ejbMethodOrServletServiceMethod();
	}

}
